package com.xinli.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.TQuestion;
import com.xinli.business.domain.TQuestionReply;
import com.xinli.business.mapper.TQuestionMapper;
import com.xinli.business.service.ITQuestionFollowService;
import com.xinli.business.service.ITQuestionReplyService;
import com.xinli.business.service.ITQuestionService;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 问题咨询Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class TQuestionServiceImpl extends ServiceImpl<TQuestionMapper, TQuestion>  implements ITQuestionService
{
    @Autowired
    private TQuestionMapper tQuestionMapper;

    /**
     * 查询问题咨询
     *
     * @param id 问题咨询主键
     * @return 问题咨询
     */
    @Override
    public TQuestion selectTQuestionById(String id)
    {
        return tQuestionMapper.selectTQuestionById(id);
    }

    /**
     * 查询问题咨询列表
     *
     * @param tQuestion 问题咨询
     * @return 问题咨询
     */
    @Override
    public List<TQuestion> selectTQuestionList(TQuestion tQuestion)
    {
        return tQuestionMapper.selectTQuestionList(tQuestion);
    }

    /**
     * 新增问题咨询
     *
     * @param tQuestion 问题咨询
     * @return 结果
     */
    @Override
    public int insertTQuestion(TQuestion tQuestion)
    {
                tQuestion.setCreateTime(DateUtils.getNowDate());
            return tQuestionMapper.insertTQuestion(tQuestion);
    }

    /**
     * 修改问题咨询
     *
     * @param tQuestion 问题咨询
     * @return 结果
     */
    @Override
    public int updateTQuestion(TQuestion tQuestion)
    {
                tQuestion.setUpdateTime(DateUtils.getNowDate());
        return tQuestionMapper.updateTQuestion(tQuestion);
    }

    /**
     * 批量删除问题咨询
     *
     * @param ids 需要删除的问题咨询主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionByIds(String[] ids)
    {
        return tQuestionMapper.deleteTQuestionByIds(ids);
    }

    /**
     * 删除问题咨询信息
     *
     * @param id 问题咨询主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionById(String id)
    {
        return tQuestionMapper.deleteTQuestionById(id);
    }

    @Autowired
    private ITQuestionReplyService questionReplyService;

    @Autowired
    private ITQuestionFollowService questionFollowService;



    @Override
    public   void setViewAndReplyCountAndReList(TQuestion question)
    {
        String questionId = question.getId();
        TQuestionReply reply = new TQuestionReply();
        reply.setQuestionId(questionId);
        List<TQuestionReply> tQuestionReplies = questionReplyService.selectTQuestionReplyList(reply);
        question.setReplyList(tQuestionReplies);
        // 评论数量
        question.setPlCount(Long.valueOf(tQuestionReplies.size()));
        long count = questionFollowService.getFollowCount(questionId);
        // 判断是否登录
        if(SecurityUtils.isLogin()){
            Boolean follow = questionFollowService.isFollow(questionId);
            question.setIsFollow(follow);
        }else {
            question.setIsFollow(null);
        }
        question.setFollowCount(count);
    }
    @Override
    public   void setViewAndReplyCount(TQuestion question)
    {
        String questionId = question.getId();
        QueryWrapper<TQuestionReply> reply = new QueryWrapper<>();
        reply.eq("question_id",questionId);
        long replycount = questionReplyService.count(reply);
        // 评论数量
        question.setPlCount(Long.valueOf(replycount));
        long count = questionFollowService.getFollowCount(questionId);
        Boolean follow = questionFollowService.isFollow(questionId);
        question.setFollowCount(count);
        question.setIsFollow(follow);
    }




    @Override
    public   void  updateViewCount(TQuestion question){
        // 观看数量加1
        UpdateWrapper<TQuestion> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", question.getId());
        updateWrapper.setSql("view_count = view_count + 1");
        this.update(updateWrapper);

    }




}
